function addVideo() {
    var title = document.getElementById("title").value;
    var description = document.getElementById("description").value;
    var file = document.getElementById('filebox_file_id_1').files[0];
    //判断控件中是否存在文件内容，如果不存在，弹出提示信息，阻止进一步操作
    if (file == null) {
        alert('请选择需要上传的视频文件');
        return;
    }

    var formData = new FormData();
    formData.append("uploadFile", file);
    formData.append("title", title);
    formData.append("description", description);

    $.ajax({
        url: "./video/save",
        type: "POST",
        data: formData,
        contentType: false,
        processData: false,
        dataType: "json",
        success: function() {
            alert("上传成功");
            $("#itemList").datagrid("reload");
            $('#addVideo').window('close');
            $('#title').textbox({value:""});
            $('#description').textbox({value:""});
            $('#uploadFile').textbox({value:""});
        },
        error: function() {
            alert("上传失败，请稍后再试");
        }
    });
}

function deleteVideo(value, title) {
    var confirm = window.confirm("你确定要删除该视频[标题:" + title + ", id: " + value + "]吗？");
    if (confirm) {
        $.ajax({
            url: "./video/delete",
            type: "POST",
            data: {id: value},
            dataType: "json",
            success: function() {
                alert("删除成功");
                $("#itemList").datagrid("reload");
            },
            error: function() {
                alert("删除失败");
            }
        });
    }
}

function playVideo(value) {
    $.ajax({
        url: "./video/get?id=" + value,
        type: "GET",
        success: function(data) {
            var data = data.data;
            document.getElementById("playTitle").innerText = data.title;
            document.getElementById("playDescription").innerText = data.description;
            document.getElementById("playUrlOgg").src = data.path;
            document.getElementById("playUrlMp4").src = data.path;
            document.getElementById("playUrl3gpp").src = data.path;
            document.getElementById("playUrl").innerText = data.path;
            $('#playVideo').window('open');
        },
        error: function() {
            alert("网络异常");
        }
    });
}

function formatVideoProgress(value, row) {
    return '<div style="float: left"><div style="margin-left: 10px; display: inline;"><a href="javascript:void(0)" onClick="deleteVideo('+ value +',\'' + row.title + '\')">删除视频</a></div>' +
            '<div style="margin-left: 20px; display: inline;"><a href="javascript:void(0)" onClick="playVideo('+ value +')">播放视频</a></div></div>';
}
